import Taro from "@tarojs/taro";
import axios from "taro-axios";
// const baseURL = `http://10.21.2.148:9000/system/`;
const baseURL = `http://192.168.43.20:9000/system/`;
export const ImageUrl = "http://192.168.43.20:9000";
const service = axios.create({
  baseURL: baseURL,
  withCredentials: true,
  timeout: 300000
});

service.interceptors.request.use(
  config => {
    const tokenValue = Taro.getStorageSync("tokenValue");
    config.headers.satoken = tokenValue;
    return config;
  },
  err => {}
);

service.interceptors.response.use(
  response => {
    if (response.data.success) {
      return response.data.data;
    } else {
      Taro.atMessage({
        message: "消息通知",
        type: "error"
      });
    }
  },
  error => {
    console.log(error.response);

    // 捕获错误信息
    if (error.response.status === 401) {
      Taro.navigateTo({
        url: "/pages/Login/index"
      });
    }
    if (error.response.status === 500) {
      Taro.atMessage({
        message: error.response.data.errorMessage,
        type: "error"
      });
    }

    return Promise.reject(error);
  }
);

export default service;
